package de.alfenory.autobill.config;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.*;

/**
 * @author alexander.fenske@alfenory.de
 */
public class DateConfiguration {
    private static final Logger log = Logger.getLogger(DateConfiguration.class.getName());
    int day;
    int month;
    int year;
    private Date currentDate;
    private Date currentDatePlus1Month;
    private Date currentDatePlus2Month;

    private Date dateAfterTerm;
    private Date dateBeforeTerm;

    private String dateFormat;
    private boolean addServicePeriod;

    public DateConfiguration(int day, int month, int year) {
        this(day, month, year, null, false);
    }

    public DateConfiguration(int day, int month, int year, String dateFormat) {
        this(day, month, year, dateFormat, true);
    }

    public DateConfiguration(int day, int month, int year, String dateFormat, boolean addServicePeriod) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        this.currentDate = cal.getTime();
        
        cal.add(Calendar.MONTH, 1);
        this.currentDatePlus1Month = cal.getTime();
        
        cal.add(Calendar.MONTH, 1);
        this.currentDatePlus2Month = cal.getTime();
        
        //Get Beforeterm Date for monthly payment
        cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);

        this.dateBeforeTerm = cal.getTime();

        //Getting last day of month
        cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        
        this.dateAfterTerm = cal.getTime();

        this.day = day;
        this.month = month;
        this.year = year;
        this.dateFormat = dateFormat;
        this.addServicePeriod = addServicePeriod;
    }
    
    public static Calendar getCalendarFirstDayOfMonth(int month,int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    public static Calendar getCalendarLastDayOfMonth(int month, int year) {
        month++;
        if(month==12) {
            month=0;
            year++;
        }
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY,-1);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    
    public Date getCurrentDate() {
        return currentDate;
    }

    public Date getCurrentDatePlus1Month() {
        return currentDatePlus1Month;
    }

    public Date getCurrentDatePlus2Month() {
        return currentDatePlus2Month;
    }

    public Date getCurrentDateMinusPeriodePlus1Day(int periode) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this.currentDate);
        cal.add(Calendar.MONTH, periode*-1);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    public int getMonth() {
        return month;
    }

    public int getYear() {
        return year;
    }

    public Date getDateAfterTerm() {
        return dateAfterTerm;
    }

    public Date getDateBeforeTerm() {
        return dateBeforeTerm;
    }

    public String getDateFormat() {
        return dateFormat;
    }

    public boolean isAddServicePeriod() {
        return addServicePeriod;
    }

    public int getDay() {
        return day;
    }
    
    public long getDaysOfPreviousPeriode(Date date, int periode) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, periode*-1);
        long daysdiff = (date.getTime() - cal.getTime().getTime()) / (1000 * 60 * 60 * 24);
        return daysdiff;
    }
}